<?php

    class RegistrationController extends AppController {

        var $uses = array( "User", "Extra", "Theme", "Registration" );


        public function beforeFilter() {

            parent::beforeFilter();

        }

        /**
         *
         */
        public function index() {

        }

        /**
         *
         */
        public function add_extra() {
            if( $this->request->is( "post" ) ) {
                $do_not_update = array();
                $this->Extra->create( $this->request->data );
                $this->Extra->set( array(
                                       'path' => '',
                                   ) );
                $target_path = "./uploads/";
                $target_path = $target_path . basename( $this->data["Extra"]['path']['name'] );
                //$themes = $this->Stages->
                if( move_uploaded_file( $this->data["Extra"]['path']['tmp_name'], $target_path ) ) {
                    $this->Extra->set( array(
                                           'path' => $target_path,
                                       ) );
                }
                else {
                    echo "There was an error uploading the file, please try again!";
                }

                if( $this->Extra->save() ) {
                    $this->Session->setFlash( "Updated", "flash_success" );
                }
                else {
                    $this->Session->setFlash( "Could not update! Please review your input and try again", "flash_error" );
                }
            }

        }

        //get registrations for current professor
        /**
         * @param $id_theme
         */
        public function get_registrations( $id_theme ) {
            $this->set( "id_theme", $id_theme );
            $this->User->id = $this->Auth->user( "id" );
            if( $this->data == null ) {
                $this->data = $this->User->read();
            }
            $regs         = $this->Registration->find( 'all', array(
                'conditions' => array(
                    'Registration.id_prof'  => $this->User->id,
                    'Registration.id_theme' => $id_theme
                )
            ) );
            $requirements = $this->Requirement->find( "all", array(
                "conditions" => array( "id_theme" => $id_theme ),
                "recursive"  => - 1
            ) );
            $n            = count( $regs );

            for( $i = 0; $i < $n; $i ++ ) {
                $r                   = $regs[$i];
                $regs[$i]['reqVals'] = array();
                foreach( $requirements as $requirement ) {
                    $reqVals                                 = $this->RequirementValue->find( "all", array(
                        "conditions" => array(
                            "id_student"     => $r['Registration']['id_user'],
                            "id_requirement" => $requirement['Requirement']['id']
                        ),
                        "recursive"  => - 1
                    ) );
                    $reqVals[0]['RequirementValue']['label'] = $requirement['Requirement']['name'];
                    $regs[$i]['reqVals']                     = array_merge( $regs[$i]['reqVals'], $reqVals );
                }
            }
            $this->set( "regs", $regs );
        }

        /**
         *
         */
        public function get_themes() {
            $this->User->id = $this->Auth->user( "id" );
            if( $this->data == null ) {
                $this->data = $this->User->read();
            }

            $themes = $this->Theme->find( 'all', array(
                'conditions' => array(
                    'Theme.id_user' => $this->User->id,
                    'Theme.id_stud' => 0
                )
            ) );
            $this->set( "themes", $themes );
        }

        /**
         * @param $id_user
         * @param $id_theme
         */
        public function accept( $id_user, $id_theme ) {
            $themes = $this->Theme->find( 'first', array( 'conditions' => array( 'Theme.id' => $id_theme ) ) );

            if( $themes['Theme']['id_stud'] < 1 ) {
                $this->Theme->create( array(
                                          'id'      => $id_theme,
                                          'id_stud' => $id_user
                                      ) );
                $this->Theme->save();
                $this->set( "accepted", 1 );
                $studs = $this->Registration->find( "all", array(
                    "conditions" => array( "id_theme" => $id_theme ),
                    "contain"    => array( "User" )
                ) );
                foreach( $studs as $stud ) {
                    if( $stud['User']['id'] != $id_user ) {
                        $this->send_email_to_student( $stud['User']['id'], "student_respins", array(
                            "stud" => $stud['User']['nume'] . " " . $stud['User']['prenume'],
                            "var1" => "Ne pare rau, dar profesorul " . $this->Auth->user( 'nume' ) . " " . $this->Auth->user( "prenume" ) . " nu ti-a acceptat inscrierea la tema " . $themes['Theme']['name']
                        ) );
                    }
                    else {
                        $this->send_email_to_student( $stud['User']['id'], "student_acceptat", array(
                            "stud" => $stud['User']['nume'] . " " . $stud['User']['prenume'],
                            "var1" => "Felicitari!<br> Profesorul " . $this->Auth->user( 'nume' ) . " " . $this->Auth->user( "prenume" ) . " ti-a acceptat inscrierea la tema " . $themes['Theme']['name']
                        ) );
                    }
                    $this->Registration->delete( $stud['Registration']['id'] );
                }
            }
            else {
                $this->set( "accepted", - 1 );
            }
        }
    }